package com.ukefu.webim.web.handler.apps.quality;

import com.ukefu.core.UKDataContext;
import com.ukefu.webim.service.acd.ServiceQuene;
import com.ukefu.webim.service.repository.*;
import com.ukefu.webim.web.handler.Handler;
import com.ukefu.webim.web.model.Quality;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/apps/qc/callevent")
public class CalleventController  extends Handler {
    @Autowired
    private QualityRepository qualityRes ;

    @Autowired
    private SessionConfigRepository sessionConfigRes ;

    @Autowired
    private TagRepository tagRes ;

    //质检服务
    @Autowired
    private  QualityRepository qualityRepository;
    /**
     * 通话质检查询
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchCallevent")
    public ModelAndView searchCallevent(ModelMap map,@RequestParam Map<String,String> parmam , HttpServletRequest request ) {
        //获取页面头部查询条件中的分值字段
        String score =  parmam.get("score");
        //使用正则校验是否是数字
        Pattern pattern = Pattern.compile("[0-9]*");
       boolean shuzi =  pattern.matcher(score).matches();
       //查询到的质检结果
        List<Quality> qualityList = null;
       //判断是否是数字
        if(shuzi){
            //将分值转成int
            Integer se = Integer.valueOf(score);
            //根据分值获取质检记录
            qualityList = qualityRes.findByScore(se);
        }else{
            //如果输入不正确则使用默认条件进行查询
            qualityList = qualityRes.findByQualitytypeAndOrgi(UKDataContext.QualityType.CHAT.toString(), super.getOrgi(request));
        }
        map.addAttribute("sessionConfig", ServiceQuene.initSessionConfig(super.getOrgi(request))) ;
        //将获取到的数据放在Mode中返回页面进行渲染
        map.addAttribute("qualityList", qualityList) ;
        map.addAttribute("tagList", tagRes.findByOrgiAndTagtype(super.getOrgi(request), UKDataContext.TagTypeEnum.QUALITY.toString())) ;
        System.err.print(parmam);
        return request(super.createAppsTempletResponse("/apps/quality/callevent/index"));
    }
}
